package org.framework.common.api.websocket.config;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
import org.springframework.web.socket.server.HandshakeInterceptor;

/**
 * 配置 Spring websocket
 * <p>描述: </p>
 * <p>Company: Professional</p>
 * @author chanlong(陈龙)
 * @date 2016年5月16日 下午12:31:07
 * @version 1.0.2016
 */
@Configuration @EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer {

	@Resource(name = "notify")
	private WebSocketHandler notify;
	
	@Resource(name = "synchs")
	private WebSocketHandler synchs;
	
	@Autowired
	private HandshakeInterceptor interceptor;
	
	/**
	 * 注册websocket监听
	 * @see org.springframework.web.socket.config.annotation.WebSocketConfigurer#registerWebSocketHandlers(org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry)
	 * @author chanlong(陈龙)
	 * @date 2016年5月16日 下午12:53:22
	 */
	@Override
	public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
		registry.addHandler(notify, "/origin/websocket").addInterceptors(interceptor).setAllowedOrigins("*");  
        registry.addHandler(notify, "/sockjs/websocket").addInterceptors(interceptor).withSockJS();
		registry.addHandler(notify, "/origin/websocket/notify").addInterceptors(interceptor).setAllowedOrigins("*");  
        registry.addHandler(notify, "/sockjs/websocket/notify").addInterceptors(interceptor).withSockJS();
        registry.addHandler(synchs, "/origin/websocket/synchs").addInterceptors(interceptor).setAllowedOrigins("*");  
        registry.addHandler(synchs, "/sockjs/websocket/synchs").addInterceptors(interceptor).withSockJS();
	}
}
